package com.feature.shop.auth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import com.feature.core.constant.SecurityConstants;
import com.feature.core.domain.Response;
import com.feature.core.util.AssertUtil;
import com.feature.core.util.WebUtil;
import com.feature.shop.auth.model.request.LoginRequest;
import com.feature.shop.auth.model.vo.LoginVo;
import com.feature.shop.auth.service.AuthService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;

@RestController
public class AuthController {

	@Autowired
	private AuthService authService;

	@PostMapping("/login")
	public Response<LoginVo> login(@Valid @RequestBody LoginRequest req, HttpServletRequest servletRequest) {
		LoginVo loginVo = authService.login(req, WebUtil.getIpAddress(servletRequest));
		return Response.ok(loginVo);
	}

	@PostMapping("/logout")
	public Response<?> logout(@RequestHeader(SecurityConstants.AUTHENTICATION) String accessToken) {
		AssertUtil.notBlank(accessToken, "accessToken不能为空");
		authService.logout(accessToken);
		return Response.ok();
	}

	@PostMapping("/refreshToken")
	public Response<?> refreshToken(String refreshToken, HttpServletRequest servletRequest) {
		AssertUtil.notBlank(refreshToken, "refreshToken不能为空");
		return Response.ok(authService.refreshToken(refreshToken, WebUtil.getIpAddress(servletRequest)));
	}
}
